from typing import List


def grid_dfs(grid, r, c):
    rows = len(grid)
    cols = len(grid[0])
    grid[r][c] = "0"
    if c + 1 <= cols - 1 and grid[r][c + 1] == "1":
        grid_dfs(grid, r, c + 1)
    if r + 1 <= rows - 1 and grid[r + 1][c] == "1":
        grid_dfs(grid, r + 1, c)
    if c - 1 >= 0 and grid[r][c - 1] == "1":
        grid_dfs(grid, r, c - 1)
    if r - 1 >= 0 and grid[r - 1][c] == "1":
        grid_dfs(grid, r - 1, c)


def numIslands(grid: List[List[str]]) -> int:
    rows = len(grid)
    if rows == 0:
        return 0
    else:
        cols = len(grid[0])
    cols = len(grid[0])
    num = 0
    for r in range(rows):
        for c in range(cols):
            if grid[r][c] == "1":
                num += 1
                grid_dfs(grid, r, c)
    return num
